package servletsWithoutStruts;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.ContactGroup;
import domain.DAOContact;
import domain.IDAOContact;
import domain.IDAOContactGroup;

/**
 * Servlet implementation class ModifyContactServlet
 */
public class ModifyContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ModifyContactServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		IDAOContact dao = (IDAOContact) context.getBean("daocontact");
		String id = request.getParameter("id");
		request.setAttribute("contact", dao.getContact(Long.valueOf(id)));
		IDAOContactGroup daog = (IDAOContactGroup) context.getBean("daocontactgroup");
		request.setAttribute("groups", daog.getContactGroups().toArray());
		Contact contact = dao.getContact(Long.valueOf(id));
		RequestDispatcher reqDis = null;
		if ( contact != null ) {
			request.setAttribute("contact", contact );
			reqDis = request.getRequestDispatcher("modifyContact.jsp");
		}
		else {
			request.setAttribute("errorMessage", "The contact doesn't exist.");
			reqDis = request.getRequestDispatcher("errorPage.jsp");			
		}
		reqDis.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		IDAOContact dao = (IDAOContact) context.getBean("daocontact");
		String numSiret = null;
		ContactGroup contactGroup = null;
		long id = Long.valueOf(request.getParameter("id"));
		String firstName = request.getParameter("firstName");
		String lastName  =  request.getParameter("lastName");
		String email  =  request.getParameter("email");
		String street = request.getParameter("street");
		String zip  =  request.getParameter("zip");
		String city  =  request.getParameter("city");
		String country  =  request.getParameter("country");
		String society = request.getParameter("entreprise");
		String[] groupIds = request.getParameterValues("groupIds");
		ArrayList<Long> groupIdsList = new ArrayList<Long>();
		if(groupIds != null){
			for(String groupId : groupIds)
				groupIdsList.add(Long.valueOf(groupId));
		}
		if(society != null){
			numSiret = request.getParameter("numSiret");
		}
		Boolean success = dao.modifyContact(id, firstName, lastName, email, zip, city, country, street, groupIdsList, numSiret, 0);
		RequestDispatcher reqDis = null;
		if(success){
			reqDis =  request.getRequestDispatcher("successPage.jsp");
			request.setAttribute("successMessage", "The contact has been updated.");
		} else {
			reqDis = request.getRequestDispatcher("errorPage.jsp");
			request.setAttribute("errorMessage", "The contact could not be updated.");
		}
		reqDis.forward(request, response);
	}

}
